package com.jse.mvc;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jse.service.NashornService;
import com.jse.view.JseView;


@Controller
public class DefaultAction {
	
	Logger log = Logger.getLogger(NashornService.class);
	
	@Resource BeetlGroupUtilConfiguration beetlConfig;
	@Resource JdbcTemplate jdbcTemplate;
	@Resource NashornService nashornService;
	
	@RequestMapping(value= {"/","/index","/index.html"})
	public Object index(@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		Object obj=nashornService.run(null,"index", p, req, resp);
		return JseView.view("index",null,obj, req, resp);
    }
	@RequestMapping(value= {"/{func:^[a-z][a-zA-Z0-9_]+$}",
			"/{func:^[a-z][a-zA-Z0-9_]+.html$}"})
    public Object func(@PathVariable String func,@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		Object obj=nashornService.run(null, func.replace(".html", ""), p, req, resp);
		return JseView.view(func.replace(".html", ""),null,obj, req, resp);
    }
	
	@RequestMapping(value={"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{id:\\d+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{id:\\d+.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{id:\\d+$}/{key:^[a-z][a-zA-Z0-9_]+$}/{val}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{id:^[a-z0-9]{30,130}$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{id:^[a-z0-9]{30,130}.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{id:\\d+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{id:\\d+.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{id:^[a-z0-9]{30,130}$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{id:^[a-z0-9]{30,130}.html$"})
    public Object jjs_id(@PathVariable String jjs,@PathVariable("id") String id,
    		@PathVariable(name="key",required=false) String key,@PathVariable(name="val",required=false) String val,
    		@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		p.addv("id", id.replace(".html", ""));
		if(!Strings.isEmpty(key))p.addv(key, val);
		Object obj=nashornService.run(jjs, jjs, p, req, resp);
		return JseView.view(jjs,jjs,obj, req, resp);
    }
	@RequestMapping(value={"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{id:\\d+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{id:\\d+.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{id:\\d+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{id:\\d+.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{id:^[a-z0-9]{30,130}$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{id:^[a-z0-9]{30,130}.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{id:^[a-z0-9]{30,130}$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{id:^[a-z0-9]{30,130}.html$}",})
    public Object jjs_func_id(@PathVariable String jjs,@PathVariable String func,@PathVariable("id") String id,
    		@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		p.put("id", id.replace(".html", ""));
		Object obj=nashornService.run(jjs, func, p, req, resp);
		return JseView.view(jjs,func,obj, req, resp);
    }
	
	@RequestMapping(value={"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{name:[^\\x00-\\xff]}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{name:[^\\x00-\\xff]}.html"})
	public Object jjs_name(@PathVariable String jjs,@PathVariable String func,@PathVariable("name") String name,@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		p.addv("name", name.replace(".html", ""));
		Object obj=nashornService.run(jjs, jjs, p, req, resp);
		return JseView.view(jjs,jjs,obj, req, resp);
    }
	@RequestMapping(value={
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{name:[^\\x00-\\xff]}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{name:[\\u4e00-\\u9fa5].html}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{name:[^\\x00-\\xff]}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{name:[\\u4e00-\\u9fa5].html"})
	public Object jjs_func_name(@PathVariable String jjs,@PathVariable String func,@PathVariable("name") String name,@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		p.addv("name", name.replace(".html", ""));
		Object obj=nashornService.run(jjs, jjs, p, req, resp);
		return JseView.view(jjs,jjs,obj, req, resp);
    }
	@RequestMapping(value= {"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+.html$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+.html$}"})
    public Object jjs_func(@PathVariable String jjs,@PathVariable(name="func") String func,@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		Object obj=nashornService.run(jjs, func.replace(".html", ""), p, req, resp);
		return JseView.view(jjs,func,obj, req, resp);
    }
	@RequestMapping(value= {"/{jjs:^[a-z][a-zA-Z0-9_]+$}/","/{jjs:^[a-z][a-zA-Z0-9_]+$}/index",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}/index.html"})
	public Object jjs_index(@PathVariable String jjs,@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		Object obj=nashornService.run(jjs, "index", p, req, resp);
		return JseView.view(jjs,"index",obj, req, resp);
    }
	@RequestMapping(value={"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{key:^[a-z][a-zA-Z0-9_]+$}/{val}",
			"/{jjs:^[a-z][a-zA-Z0-9_]+$}-{func:^[a-z][a-zA-Z0-9_]+$}-{key:^[a-z][a-zA-Z0-9_]+$}-{val}"})
    public Object jjs_func_kv(@PathVariable String jjs,@PathVariable String func,
    		@PathVariable(name="key") String key,@PathVariable(name="val") String val,
    		@RequestAttribute("p")NutMap p,HttpServletRequest req,HttpServletResponse resp) throws Throwable{
		p.addv(key, val);
		Object obj=nashornService.run(jjs, func, p, req, resp);
		return JseView.view(jjs,func,obj, req, resp);
    }
	@RequestMapping(value={"/{jjs:^[a-z][a-zA-Z0-9_]+$}/{func:^[a-z][a-zA-Z0-9_]+$}/{id}/{key:^[a-z][a-zA-Z0-9_]+$}/{val}"})
	public Object jjs_func_kvid(@PathVariable String jjs, @PathVariable String func,
			@PathVariable(name = "id") String id, @PathVariable(name = "key") String key,
			@PathVariable(name = "val") String val, @RequestAttribute("p") NutMap p, HttpServletRequest req,
			HttpServletResponse resp) throws Throwable {
		p.addv("id", id);
		p.addv(key, val);
		Object obj = nashornService.run(jjs, func, p, req, resp);
		return JseView.view(jjs, func, obj, req, resp);
	}
	
}
